{
 "cells": [
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "<img src=\"LayerNorm.png\" alt=\"描述\" style=\"margin-left: auto; margin-right: auto; width:30%; height:auto; border-radius:10px;\">",
   "id": "ae769e4e9e42a70a"
  },
  {
   "metadata": {
    "collapsed": true,
    "ExecuteTime": {
     "end_time": "2025-11-03T06:06:07.607038Z",
     "start_time": "2025-11-03T06:06:07.585490Z"
    }
   },
   "cell_type": "code",
   "source": [
    "import torch\n",
    "from torch import nn\n",
    "class LayerNorm(nn.Module):\n",
    "    def __init__(self, d_model, eps=1e-5):\n",
    "        super(LayerNorm, self).__init__()\n",
    "        self.eps = eps\n",
    "        self.gamma = nn.Parameter(torch.ones(d_model))\n",
    "        self.beta = nn.Parameter(torch.zeros(d_model))\n",
    "\n",
    "    def forward(self, x):\n",
    "        mean = x.mean(-1, keepdim=True)\n",
    "        var = x.var(-1,unbiased=False, keepdim=True)\n",
    "        out = (x - mean) / torch.sqrt(var + self.eps)\n",
    "        out = self.gamma * out + self.beta\n",
    "        return out"
   ],
   "id": "initial_id",
   "outputs": [],
   "execution_count": 5
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-03T06:09:29.756381Z",
     "start_time": "2025-11-03T06:06:10.970757Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 创建 LayerNorm 实例\n",
    "d_model = 512\n",
    "layer_norm = LayerNorm(d_model)\n",
    "\n",
    "# 创建测试输入数据\n",
    "batch_size = 2\n",
    "seq_len = 10\n",
    "# 输入形状: [batch_size, seq_len, d_model]\n",
    "x = torch.randn(batch_size, seq_len, d_model)\n",
    "\n",
    "# 应用 LayerNorm\n",
    "output = layer_norm(x)\n",
    "\n",
    "# 打印结果\n",
    "print(f\"Input shape: {x.shape}\")\n",
    "print(f\"Output shape: {output.shape}\")\n",
    "print(f\"Input mean: {x.mean():.4f}, std: {x.std():.4f}\")\n",
    "print(f\"Output mean: {output.mean():.4f}, std: {output.std():.4f}\")"
   ],
   "id": "f520419210fdbd0",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Input shape: torch.Size([2, 10, 512])\n",
      "Output shape: torch.Size([2, 10, 512])\n",
      "Input mean: 0.0061, std: 0.9954\n",
      "Output mean: -0.0000, std: 1.0000\n"
     ]
    }
   ],
   "execution_count": 15
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
